﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using School.Entities;
using School.Common;
using School.Admission.ReportsCls;

namespace School.Admission
{
    public class GenerateReport
    {
        private SchoolEntities db = new SchoolEntities();

        public IEnumerable<NormalTransferObject> NormalTransfer(long id)
        {
            List<NormalTransferObject> rows = new List<NormalTransferObject>();

            var data = from x in db.TE_Student
                       where x.Id == id
                       select x;

            string year = (from x in db.ORG_EductionalYear where x.IsCurrent == true select x.name).First();

            foreach (var x in data)
            {
                var item = new NormalTransferObject
                {
                    StudentName = x.Ar_FirstName + " " + x.Ar_SecondName + " " + x.Ar_ThirdNmae + " " + x.Ar_FourthName,
                    Birthdate = x.Gregorian_BirthDate,
                    Grade = x.ORG_Grade.Name,
                    EducationalYear = year,
                };

                try
                {
                    item.FatherName = x.SD_Father.Select(y => y.Ar_FirstName + " " + y.Ar_SecondName + " " + x.Ar_ThirdNmae + " " + x.Ar_FourthName).First();
                }
                catch
                {
                    item.FatherName = x.Ar_SecondName + " " + x.Ar_ThirdNmae + " " + x.Ar_FourthName;
                }

                try
                {
                    item.FatherAddress = x.SD_Father.Select(y => y.SD_HomeAddress.StreetName).First();
                }
                catch
                {
                    item.FatherAddress = "";
                }

                try
                {
                    item.FatherWork = x.SD_Father.Select(y => y.LU_Jobs.Name).First();
                }
                catch
                {
                    item.FatherWork = "";
                }

                rows.Add(item);
            }

            return rows;
        }

        public IEnumerable<StudentInfo> StudentsData(long grade)
        {
            var students = from x in db.TE_Student where x.Grade_ID == grade select x;

            List<StudentInfo> si = new List<StudentInfo>();

            foreach (var s in students)
            {
                StudentInfo st = new StudentInfo();

                st.StudentName = s.Ar_FirstName + " " + s.Ar_SecondName + " " + s.Ar_ThirdNmae + " " + s.Ar_FourthName;
                st.GradeName = s.ORG_Grade.Name;

                try
                {
                    st.Address = s.SD_Father.First().SD_HomeAddress.StreetName;
                }
                catch
                {
                    st.Address = " ";
                }

                try
                {
                    st.State = s.SD_Father.First().SD_HomeAddress.LU_Stat.Name;
                }
                catch
                {
                    st.State = " ";
                }

                st.Birthdate = s.Gregorian_BirthDate;

                try
                {
                    st.NationalId = s.SD_Father.First().Identification_Number;
                }
                catch
                {
                    st.NationalId = " ";
                }

                try
                {
                    st.Status = s.ADM_Status_Type.Name;
                }
                catch
                {
                    st.Status = " ";
                }

                if (st.Birthdate != null)
                {
                    var endDate = new DateTime(DateTime.Now.Year, 10, 1);
                    var startDate = DateTime.Parse(st.Birthdate.ToString());

                    string timeStr = string.Empty;
                    int yr = 0;
                    int mth = 0;
                    int days = 0;

                    TimeSpan ts = new TimeSpan();
                    ts = endDate.Subtract(startDate);
                    yr = (ts.Days / 365);

                    do
                    {
                        for (int i = 0; i <= 12; i++)
                        {
                            if (endDate.Subtract(startDate.AddYears(yr).AddMonths(i)).Days > 0)
                            {
                                mth = i;
                            }
                            else
                            {
                                break;
                            }
                        }

                        if (mth > 12)
                            yr = yr + 1;
                    } while (mth > 12);

                    days = endDate.Subtract(startDate.AddYears(yr).AddMonths(mth)).Days;

                    st.OctoberDay = days.ToString();
                    st.OctoberMonth = mth.ToString();
                    st.OctoberYear = yr.ToString();                    
                }


                si.Add(st);


            }
            return si;
        }
    }
}